// custom-tab-bar/index.js
const app = getApp();
var home = require("../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  
    data: {
      index1: '../image/tabbar/index_def.png',
      index2: '../image/tabbar/index_xz.png',
      shop1: '../image/tabbar/shop_def.png',
      shop2: '../image/tabbar/shop_xz.png',
      car1: '../image/tabbar/car_def.png',
      car2: '../image/tabbar/car_xz.png',
      car3: '../image/tabbar/car_def.png',
      my1: '../image/tabbar/my_def.png',
      my2: '../image/tabbar/my_xz.png',
      my3: '../image/tabbar/my_def.png',
      show1: true,
      show2: false,
      show3: false,
      show4: false,
      state: wx.getStorageSync('state'),
      btnState: true,
      btnState1: true,
      bgcolor: home.getKey('bgcolor'),
    },
  


  /**
   * 组件的方法列表
   */
  methods: {
    indexBtn() {
      wx.switchTab({
        url: '../index/index',
      })
    },
    shopBtn() {
      wx.switchTab({
        url: '../shop/index',
      })
    },
    carBtn() {
      wx.switchTab({
        url: '../car/index',
      })
    },
    myBtn() {
      wx.switchTab({
        url: '../user/index',
      })
    },
    getPhoneNumber(e) {
      console.log(e.detail);
      wx.showLoading({
        title: '加载中...',
      });
      this.setData({
        btnState1:false,
      })
      let that = this;
      wx.request({
        url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/getPhone?storeId=' + app.globalData.storeId + '&code=' + e.detail.code + '&xcxOpenId=' + wx.getStorageSync('openId') + '&unionId=' + wx.getStorageSync('unionId'),
        data: {
          storeId: app.globalData.storeId,
          code: e.detail.code,
          xcxOpenId:wx.getStorageSync('openId'),
          unionId:wx.getStorageSync('unionId')
        },
        method: 'POST',
        header: {
          "content-type": "application/json", // 默认
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.hideLoading({
              success: (res) => {
                that.setData({
                  btnState1: true,
                })
              },
            })
            wx.setStorageSync('phone', res.data.result.phone);
            wx.setStorageSync('userId', res.data.result.id);
            let state = 2;
            if (!wx.getStorageSync('hasNickName')) {
              state = 0;
            }
            that.setData({
              state: state
            });
            wx.setStorageSync('state', state);
            wx.reLaunch({
              url: '../index/index',
            })
          } else {
            wx.showToast({
              title: '获取失败',
              icon: 'none',
              duration: 1500,
            })
          }
        }
      })
    },
    //小程序授权
    login(e) {
      wx.showLoading({
        title: '加载中...',
      })
      this.setData({
        btnState:false,
      });
      let that = this;
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: function (ress) {
          wx.login({
            success(res) {
              wx.request({
                url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/updateUserNickName?storeId=' + app.globalData.storeId + '&code=' + res.code + '&xcxOpenId=' + wx.getStorageSync('openId') + '&unionId=' + wx.getStorageSync('unionId') + '&nickName' + ress.userInfo.nickName + '&headPortrait=' + ress.userInfo.avatarUrl + '&sex=保密',
                data: {
                  storeId: app.globalData.storeId,
                  nickName: ress.userInfo.nickName,
                  headPortrait: ress.userInfo.avatarUrl,
                  sex: '保密',
                  xcxOpenId: wx.getStorageSync('openId'),
                  unionId: wx.getStorageSync('unionId')
                },
                method: 'POST',
                header: {
                  "content-type": "application/json", // 默认
                },
                success: function (rea) {
                  if (rea.data.code == 200) {
                    wx.hideLoading({
                      success: (res) => {
                        that.setData({
                          btnState: true,
                        })
                      },
                    })
                    wx.setStorageSync('nickName', ress.userInfo.nickName);
                    wx.setStorageSync('avatar', ress.userInfo.avatarUrl);
                    that.setData({
                      state: 2
                    });
                    wx.setStorageSync('state', 2);
                    wx.reLaunch({
                      url: '../index/index',
                    })
                  }
                }
              })
            }
          })
        },
        fail: function (res) {
          that.setData({
            btnState: true,
          })
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 1500,
          })
        }
      })
    },
  },
})